import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

class DioCase extends StatefulWidget {
  const DioCase({Key? key}) : super(key: key);

  @override
  _DioCaseState createState() => _DioCaseState();
}

class _DioCaseState extends State<DioCase> {
  List _channels = [];

  @override
  void initState() {
    // TODO: implement initState
    print('initState');
    getAllChannels();
    super.initState();
  }

  getAllChannels() async {
    String BASE_URL = "https://geek.itheima.net";
    Dio dio = Dio();
    var res = await dio.get('$BASE_URL/v1_0/channels');
    setState(() {
      _channels = res.data['data']['channels'];
    });
    // print(res.data['data']['channels']);
  }

  @override
  Widget build(BuildContext context) {
    return GridView(
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 4,
        mainAxisSpacing: 10,
        crossAxisSpacing: 10,
      ),
      children: _channels.map((dynamic item) {
        return Container(
          color: Colors.lightBlue,
          alignment: Alignment.center,
          margin: const EdgeInsets.all(10),
          child: Text(
            item['name'],
            style: const TextStyle(color: Colors.white, fontSize: 20),
          ),
        );
      }).toList(),
    );
  }
}
